package com.guaju.yueshangLife.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import com.githang.statusbar.StatusBarCompat;
import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.base.MyAppcomActivity;
import com.guaju.yueshangLife.databinding.ActivitySetupPasswordBinding;
import com.guaju.yueshangLife.event.CodeEvent;
import com.guaju.yueshangLife.net.ApiClient;
import com.guaju.yueshangLife.viewmodel.login.SetupPasswordVM;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

public class SetupPasswordActivity extends MyAppcomActivity {
    private ActivitySetupPasswordBinding binding;
    private int code;
    private String msg;
    boolean is_eye1_open = false;
    boolean is_eye2_open = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_setup_password);
        EventBus.getDefault().register(this);
        //一体栏
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.home_background));

        SetupPasswordVM setupPSVM = new SetupPasswordVM();
        binding.setViewmodel(setupPSVM);
        initView();
        setListener();
    }

    private void setListener() {
        binding.flEye1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (is_eye1_open) {
                    is_eye1_open = false;
                    binding.etPassword1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    binding.ivEye1.setBackgroundResource(R.mipmap.eye_close);
                } else {
                    binding.etPassword1.setInputType(InputType.TYPE_TEXT_VARIATION_NORMAL);
                    is_eye1_open = true;
                    binding.ivEye1.setBackgroundResource(R.mipmap.eye_open);
                }

            }
        });
        binding.flEye2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (is_eye2_open) {
                    is_eye2_open = false;
                    binding.etPassword2.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    binding.ivEye2.setBackgroundResource(R.mipmap.eye_close);
                } else {
                    is_eye2_open = true;
                    binding.etPassword2.setInputType(InputType.TYPE_TEXT_VARIATION_NORMAL);
                    binding.ivEye2.setBackgroundResource(R.mipmap.eye_open);
                }
            }
        });
        binding.flBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initView() {
        binding.etPassword2.addTextChangedListener(new LoginTextWatch());
    }

    public class LoginTextWatch implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.toString().length() > 0) {
                binding.btLogin.setBackgroundResource(R.drawable.selector_login_button);
            } else {
                binding.btLogin.setBackgroundResource(R.drawable.shape_login_unable_bg);
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(String str) {
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(str);
            code = jsonObject.getInt("code");
            msg = jsonObject.getString("msg");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (200 == code) {
//            登录成功
            finish();
        } else {
            Toast.makeText(this, "设置密码失败" + msg, Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            startActivity(new Intent(this, MainActivity.class));
        }
        return super.onKeyDown(keyCode, event);
    }

}
